package com.tiantian.quanminxiu.presenter;

import android.util.Log;

import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.api.AnchorService;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.ApplicationBase;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.RegisterView;

import org.json.JSONObject;

import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by lzx on 2016/10/24.
 */

public class RegisterPresent implements Presenter<RegisterView> {
    private RegisterView registerView;
    @Override
    public void attachView(RegisterView view) {
        registerView = view;
    }

    @Override
    public void detachView() {
        registerView = null;
    }
    public void getCode(String Pnum){
        registerView.Loading_Open();
        ApplicationBase app = ApplicationBase.get(registerView.getContext());
        AnchorService service = app.getAnchorService();
        service.getCode(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE,Pnum)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .finallyDo(()->{
                    registerView.Loading_Close();
                }).subscribe(str ->{
                    String msg = str.optString("msg");
                    registerView.getPhoneCode(str.optInt("flag"),msg);
                },throwable -> {
                    CommonUtils.showToask(registerView.getContext(),throwable.getMessage());
                    Log.e("shibai",throwable.getMessage());
                });
    }
    public void register(String Phone,int VerifyCode,String Password){
        registerView.Loading_Open();
        ApplicationBase app = ApplicationBase.get(registerView.getContext());
        AnchorService service = app.getAnchorService();
        service.register(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE
                ,Phone,VerifyCode,CommonUtils.MD5(Password),"SignUp", APPUtils.getMetaInt(registerView.getContext(),"UMENG_CHANNEL"))
                .flatMap(str -> {
                    return service.open(UserData.Current.mid, Constants.APP_TYPE, UserData.Current.sign
                            , UserData.Current.Equipment_Name, UserData.Current.Screen_Width, UserData.Current.Screen_Height
                            , UserData.Current.OS_Version, UserData.Current.APP_Version, UserData.Current.APP_VersionCode
                            , 116.307629, 40.058359);
                }).flatMap(str -> {
                    UserData.Current.USERCOVER_URL = str.optJSONObject("data").optString("ImgUserCover");
                    UserData.Current.USERAVATAR_URL = str.optJSONObject("data").optString("ImgUserAvatar");
                    return service.user_userinfo_info(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE);
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .finallyDo(()->{
                    registerView.Loading_Close();
                }).subscribe(str ->{
                    JSONObject value = str.optJSONObject("data");
                    UserData.Base.NickName = value.optString("NickName");
                    UserData.Base.User_ID = value.optInt("User_ID");
                    UserData.Base.Coin = value.optInt("Coin");
                    UserData.Base.Beans = value.optInt("Beans");
                    UserData.Base.Sex = value.optInt("Sex");
                    UserData.Base.Attention = value.optInt("Attention");
                    UserData.Base.Pomelo = value.optInt("Pomelo");
                    UserData.Base.Status = value.optInt("Status");
                    UserData.Base.Level = value.optInt("Level");
                    UserData.Base.Fans = value.optInt("Fans");
                    UserData.Base.Signature = value.optString("Signature");
                    UserData.Base.isAnchor = value.optInt("IsAnchor")==1?true:false;
                    UserData.Base.IsAdmin = value.optInt("IsAdmin")==1?true:false;
                    registerView.JumpMain();
                },throwable -> {
                    CommonUtils.showToask(registerView.getContext(),throwable.getMessage());
                });
    }
}
